<?php
/**
 * Controller class
 * 
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 *
 */
abstract class Mac_Controller
{
    /**
     * Layout
     *
     * @var Mac_Layout
     */
    protected $layout;
    
    /**
     * Displayed view
     *
     * @var Mac_View
     */
    protected $view;
    
    /**
     * Associative array containing module, controller and action names, formatted for view
     * @var Array
     */
    protected $viewData = array();
    
    /**
     * HTTP request
     * 
     * @var Mac_Controller_HTTPRequest 
     */
    private $request;
    
    /**
     * Unordered GET params
     * 
     * @var array
     */
    private $params;
    
    public function __construct (Mac_Controller_HTTPRequest $request)
    {
        $this->request = $request;
        $this->params = $request->getUnorderedParams();
        $this->layout = new Mac_Layout();
    }

	/**
	 * Method to redefine in extending class Mac_called by the constructor
	 */
	public function init () {}
    
    public function getRequest () {
        return $this->request;
    }
    
    public function getLayout () {
        return $this->layout;
    }

    /**
     * Define the path to use to load display
     *
     * @param string $module The module folder name
     * @param controller $controller The controller folser name (under view folder)
     * @param string $action The action folder name (under the controller)
     * @param boolean $firstCall Indicate if the call come from the Front Controller
     */
    public function initView ($module, $controller, $action, $firstCall=true)
    {
        $this->viewData = array(
            'module' => $module,
            'controller' => $controller,
            'action' => $action
        );
        $this->layout->initView($module, $controller, $action);
        $this->view = $this->layout->getView();

        if ($firstCall === true) {
            $this->init();
        }
    }
    
    protected function useView ($action, $_controller=null, $_module=null)
    {
        $controller = ($_controller !== null) ? $_controller : $this->viewData['controller'];
        $module = ($_module !== null) ? $_module : $this->viewData['module'];
        $this->initView($module, $controller, $action, false);
    }
    
    protected function useLayout ($layout)
    {
        $this->layout->setLayout($layout);
    }
    
    /**
     * Return a GET parameter from its name
     * 
     * @param string $name
     * @return string|array|null 
     */
    protected function getParam ($name)
    {
        if (isset($this->params[$name])) {
            return $this->params[$name];
        } else {
            return null;
        }
    }
    
    public function restrictAccess ($groups = array(), Mac_Url $redirect = null)
    {
        if (!is_array($groups)) {
            $groups = array($groups);
        }
        if ( !Mac_User::isAuthentified() || !Mac_User::isAuthorized($groups)) {
            if ($redirect !== null) {
                header('Location: '.$redirect);
                exit;
            }
            die('accès limité'); // TODO : rediriger vers un controller par default
        }
    }
}